import 'package:flutter/material.dart';
import 'package:hi_net/page/video_detail_page.dart';
import 'package:hi_net/page/home_page.dart';
import 'package:hi_net/page/login_page.dart';
import 'package:hi_net/page/main_page.dart';
import 'package:hi_net/page/register_page.dart';

/// 创建页面
pageWrap(Widget child) {
  return MaterialPage(key: ValueKey(child.hashCode), child: child);
}

///获取routeStatus在页面栈中的位置
int getPageIndex(List<MaterialPage> pages, RouteStatus routerStatus) {
  var pageIndex = -1;
  pages.asMap().forEach((index, item) {
    if (getStatus(item) == routerStatus) {
      pageIndex = index;
    }
  });
  return pageIndex;
}

///自定义路由封装，路由状态
enum RouteStatus { login, register, main, home, detail, unknown }

///获取page对应的RouteStatus
RouteStatus getStatus(MaterialPage page) {
  if (page.child is LoginPage) return RouteStatus.login;
  if (page.child is RegisterPage) return RouteStatus.register;
  if (page.child is MainPage) return RouteStatus.main;
  if (page.child is HomePage) return RouteStatus.home;
  if (page.child is VideoDetailPage) return RouteStatus.detail;
  return RouteStatus.unknown;
}

///路由信息
class RouteStatusInfo {
  final RouteStatus routeStatus;
  final Widget page;

  RouteStatusInfo(this.routeStatus, this.page);
}

///定义路由改变监听 类型定义（接口回调）
typedef RouteChangeListener = void Function(RouteStatusInfo current, RouteStatusInfo? pre);

///监听路由页面跳转
///感知当前页面是否压后台
class HiNavigator extends _RouteJumpListener {
  HiNavigator._();

  static HiNavigator? _instance;

  static HiNavigator get getInstance {
    _instance ??= HiNavigator._();
    return _instance!;
  }

  RouteJumpListener? _routeJump;
  final List<RouteChangeListener> _listeners = [];
  RouteStatusInfo? _current;

  //首页底部tab
  RouteStatusInfo? _bottomTab;

  ///首页底部tab切换监听
  void onBottomTabChange(int index, Widget page) {
    _bottomTab = RouteStatusInfo(RouteStatus.main, page);
    _notify(_bottomTab!);
  }

  ///注册路由跳转逻辑
  void registerRouteJump(RouteJumpListener routeJump) {
    _routeJump = routeJump;
  }

  ///监听路由页面跳转
  void addListener(RouteChangeListener listener) {
    if (!_listeners.contains(listener)) _listeners.add(listener);
  }

  ///移除监听
  void removeListener(RouteChangeListener listener) {
    _listeners.remove(listener);
  }

  ///通知路由页面变化
  void notify(List<MaterialPage> currentPages, List<MaterialPage> prePages) {
    if (currentPages == prePages) return;
    // 新页面
    var current = RouteStatusInfo(getStatus(currentPages.last), currentPages.last.child);
    _notify(current);
  }

  void _notify(RouteStatusInfo current) {
    if (current.page is MainPage && _bottomTab != null) {
      //如果打开的是主页，则明确到主页具体的tab
      current = _bottomTab!;
    }

    /// 遍历，通知所有路由改变监听
    for (var listener in _listeners) {
      listener(current, _current);
    }
    _current = current;
  }

  @override
  void onJumpTo(RouteStatus routeStatus, {Map? args}) {
    _routeJump?.onJumpTo(routeStatus, args: args);
  }
}

///抽象类供HiNavigator实现
abstract class _RouteJumpListener {
  void onJumpTo(RouteStatus routeStatus, {Map? args});
}

///定义页面跳转 类型定义（接口回调）
typedef OnJumpTo = void Function(RouteStatus routeStatus, {Map? args});

///定义路由跳转逻辑要实现的功能
class RouteJumpListener {
  final OnJumpTo onJumpTo;

  RouteJumpListener({required this.onJumpTo});
}
